﻿using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using WMS.RepositoryDefinitions;
using WMS.RepositoryDefinitions.Dto;

namespace WMS.Web.Controllers
{
    public class RepositoryController : Controller
    {
        private readonly IRepositoryDefinitionService _repository;

        public RepositoryController(IRepositoryDefinitionService repository)
        {
            _repository = repository;
        }

        public IActionResult Add()
        {
            return View();
        }

        public IActionResult RepositoryList()
        {
            return View();
        }

        public IActionResult Update(int id)
        {
            ViewBag.id = id;
            return View();
        }

        public async Task<int> AddRepository(RepositoryDefinitionDto repositoryDefinitionDto)
        {
            var result = await _repository.AddRepository(repositoryDefinitionDto);
            return result;
        }

        public async Task<JsonResult> GetRepositoryList(string? repositoryCode, int page = 1, int limit = 10)
        {
            var result = await _repository.GetRepositoryList(repositoryCode, page, limit);
            return Json(new { data = result.Queryable, count = result.RowCount, code = 0 });
        }

        public async Task<int> DeleteRepository(int id)
        {
            var result = await _repository.DeleteRepository(id);
            return result;
        }

        public async Task<JsonResult> GetRepositoryInfo(int id)
        {
            var list = _repository.GetRepositoryInfo(id);
            return Json(new { data = list.Result });

        }

        public async Task<int> UpdateRepository(RepositoryDefinitionDto repositoryDefinitionDto)
        {
            var result = await _repository.UpdateRepository(repositoryDefinitionDto);
            return result;
        }
    }
}
